﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Telnet
{
    internal partial class ListenTelnetClient
    {
        /*
        private string _receivedData;
        private void ProcessReceiveData_(byte[] data, int revicedDataLength)
        {
            if (data[0] >= 0xF0)
            {
                return;
            }

            string receivedData = this._receivedData;
            // 0x2E = '.', 0x0D = carriage return, 0x0A = new line
            if (revicedDataLength >= 2 &&
                ((data[0] == 0x2E && data[1] == 0x0D && receivedData.Length == 0) ||
                (data[0] == 0x0D && data[1] == 0x0A)))
            {
                //this.SendMessage("\u001B[1J\u001B[H");//回显清空
                this.SendMessage(TelnetConstant.END_LINE_CURSOR);//回显换行
                string result = this._processMessage(receivedData);
                result += TelnetConstant.END_LINE_CURSOR;
                this._receivedData = string.Empty;
                this.SendMessage(result);
            }
            else
            {
                if (data[0] == 0x08)// 0x08 => backspace character
                {
                    if (receivedData.Length > 0)
                    {
                        this._receivedData = this._receivedData.Substring(0, receivedData.Length - 1);
                        this.Send(new byte[] { 0x08, 0x20, 0x08 });
                    }
                    else
                    {
                        return;
                    }
                }
                else if (data[0] == 0x7F)// 0x7F => delete character
                {
                    return;
                }
                else
                {
                    this._receivedData += this._messageEncoding.GetString(data, 0, revicedDataLength);

                    // Echo back the received character,if client is not writing any password
                    if (this._status != EClientStatus.Authenticating)
                    {
                        this.Send(new byte[] { data[0] });
                    }
                    else// Echo back asterisks if client is writing a password
                    {
                        this.SendMessage("*");
                    }
                }
            }
        }

        private void ProcessReceiveData_bk2(byte[] data, int revicedDataLength)
        {
            if (revicedDataLength <= 0 || data[0] >= 0xF0)
            {
                return;
            }


            if (data[0] == 0x08)// 0x08 => backspace character
            {
                if (this._sb.Length > 0)
                {
                    this._sb.Remove(this._sb.Length - 1, 1);
                    this.Send(new byte[] { 0x08, 0x20, 0x08 });
                }
                else
                {
                    return;
                }
            }
            else if (data[0] == 0x7F)// 0x7F => delete character
            {
                return;
            }
            else
            {
                //this._receivedData += this._messageEncoding.GetString(data, 0, revicedDataLength);

                //// Echo back the received character,if client is not writing any password
                //if (this._status != EClientStatus.Authenticating)
                //{
                //    this.Send(new byte[] { data[0] });
                //}
                //else// Echo back asterisks if client is writing a password
                //{
                //    this.SendMessage("*");
                //}

                this.Send(data, 0, revicedDataLength);

                string inputStr = this._messageEncoding.GetString(data, 0, revicedDataLength);
                //ZLoger.Info($"revicedDataLength:{revicedDataLength}  {inputStr}");                

                if (inputStr.EndsWith("\r\n") ||
                    inputStr.EndsWith("\r") ||
                    inputStr.EndsWith("\n") ||
                    inputStr.EndsWith("\r\0"))
                {
                    if (this._sb.Length > 0)
                    {
                        string message = this._sb.ToString();
                        if (string.Equals(message, "-q", StringComparison.OrdinalIgnoreCase))
                        {
                            this.OnRaiseDisconnect();
                            return;
                        }

                        string result = this._processMessage(message);
                        result = $"{TelnetConstant.CURSOR}{result}{TelnetConstant.END_LINE_CURSOR}";
                        this._sb.Clear();
                        this.SendMessage(result);
                    }
                    else
                    {
                        this.SendMessage(TelnetConstant.CURSOR);
                    }
                }
                else
                {
                    this._sb.Append(inputStr);
                }
            }
        }

        private void ProcessReceiveData_bk(byte[] data, int revicedDataLength)
        {
            if (revicedDataLength <= 0 || data[0] >= 0xF0)
            {
                return;
            }




            //this._receivedData += this._messageEncoding.GetString(data, 0, revicedDataLength);

            //// Echo back the received character,if client is not writing any password
            //if (this._status != EClientStatus.Authenticating)
            //{
            //    this.Send(new byte[] { data[0] });
            //}
            //else// Echo back asterisks if client is writing a password
            //{
            //    this.SendMessage("*");
            //}

            this.Send(data, 0, revicedDataLength);


            if (revicedDataLength == 2 && data[0] == 13 && data[1] == 0 ||//linux
               revicedDataLength == 2 && data[0] == 13 && data[1] == 10)//windows
            {
                //enter key
                if (this._sb.Length > 0)
                {
                    string message = this._sb.ToString().Trim();
                    this._historyCommandList.Add(message);
                    this._historyCommandIndex = -1;
                    this._sb.Clear();

                    if (string.IsNullOrWhiteSpace(message))
                    {
                        return;
                    }

                    //内部命令处理
                    if (this.ProcessInnerCommand(message))
                    {
                        return;
                    }

                    //外部命令处理
                    string result = this._processMessage(message);
                    result = $"{TelnetConstant.CURSOR}{result}{TelnetConstant.END_LINE_CURSOR}";
                    this.SendMessage(result);
                }
                else
                {
                    this.SendMessage(TelnetConstant.CURSOR);
                }
            }
            else
            {
                foreach (var command in TelnetKeyCommandManager.Commands)
                {
                    if (command.Process(this, data, revicedDataLength))
                    {
                        return;
                    }
                }

                string inputStr = this._messageEncoding.GetString(data, 0, revicedDataLength);
                this._sb.Append(inputStr);
            }
        }
        */
    }
}
